/*******************************************************************************
 * Package: com.hngtrust.iir.user.controller
 * Type:    CollectController
 * Date:    2021/3/16 16:40
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.framework.page.service.PageService;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.company.service.CompanyService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.risk.service.RiskMonitorService;
import com.hngtrust.iir.user.controller.dto.*;
import com.hngtrust.iir.user.dto.CollectGroupVO;
import com.hngtrust.iir.user.dto.ListCollectInfoRespVO;
import com.hngtrust.iir.user.entity.CollectGroup;
import com.hngtrust.iir.user.entity.CollectInfo;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.CollectGroupService;
import com.hngtrust.iir.user.service.CollectInfoService;
import com.hngtrust.iir.user.service.CollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 信息收藏管理
 *
 * @author Chaidd
 * @date 2021/12/17 16:40
 */
@Slf4j
@Api(tags = "信息收藏管理")
@RestController
@RequestMapping("/collectInfo")
public class CollectInfoController {
    @Autowired
    private CollectService collectService;

    @Autowired
    private CollectInfoService collectInfoService;

    @Autowired
    private CollectGroupService collectGroupService;
    
    @Autowired
    private CompanyService companyService;
    
    @Autowired
    private RiskMonitorService riskMonitorService;
    
    @Autowired
    private PageService pageService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @Autowired
    private LogService logService;
    
    @ApiOperation("收藏列表")
    @PostMapping("/list-collect")
    public ResponseInfo<PageResult<ListCollectInfoRespVO>> listCollect(
            @ApiParam(value = "收藏列表") @Valid @RequestBody ReqDTO<ListCollectInfoVO> dto){
//        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
//
//        PageResult<ListCollectInfoRespVO> page = pageService
//                .page(dto.getPageParam(), () -> collectInfoService.queryListCollect(loginUser.getId(),dto.getContent().getGroupId()));
//
//        return new ResponseInfo<>(page);
        return null;
    }
    
    
    @ApiOperation("添加收藏")
    @PostMapping("/add-collect")
    public ResponseInfo<Object> addCollect(@ApiParam(value = "添加参数") @Valid @RequestBody CollectInfoReqVO collectVO){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        CollectInfo collectInfo = new CollectInfo();
        collectInfo.setId(myIdGenerator.nextId());
        collectInfo.setInfoType(collectVO.getInfoType());
        collectInfo.setInfoId(collectVO.getInfoId());
        collectInfo.setInfoName(collectVO.getInfoName());
        collectInfo.setGroupingId(collectVO.getGroupingId());
        collectInfo.setCreateTime(new Date());
        collectInfo.setCreateUserId(loginUser.getId());
        collectInfoService.add(collectInfo);
        //添加日志
        OptLogReqVO optLog = new OptLogReqVO();
        optLog.setOptModule(Constant.LOG_OPTMODULE_FAVORITE);
        optLog.setOptType(Constant.LOG_OPTTYPE_ADD);
        optLog.setStatus(Constant.LOG_OPTSTATE_SUCCESS);
        String infoType = collectVO.getInfoType() == '1' ? "资讯" :(collectVO.getInfoType() == '2' ? "专家观点" : "研报" );
        optLog.setOptContent("收藏"+infoType+collectVO.getInfoName());
        logService.saveOptLog(optLog);
        
        return new ResponseInfo<Object>();
    }
    
    
    @ApiOperation("删除收藏")
    @PostMapping("/delete-collect")
    public ResponseInfo<Object> deleteCollect(@ApiParam(value = "删除参数") @Valid @RequestBody DeleteCollectInfoReqVO collectVO){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        CollectInfo collectInfo = new CollectInfo();
        collectInfo.setCreateUserId(loginUser.getId());
        collectInfo.setInfoType(collectVO.getInfoType());
        collectInfo.setInfoId(collectVO.getInfoId());
        collectInfoService.delete(collectInfo);
        //添加日志
        OptLogReqVO optLog = new OptLogReqVO();
        optLog.setOptModule(Constant.LOG_OPTMODULE_FAVORITE);
        optLog.setOptType(Constant.LOG_OPTTYPE_DEELTE);
        optLog.setStatus(Constant.LOG_OPTSTATE_SUCCESS);
        String infoType = collectVO.getInfoType() == '1' ? "资讯" :(collectVO.getInfoType() == '2' ? "专家观点" : "研报" );
        optLog.setOptContent("删除"+infoType+collectVO.getInfoName());
        logService.saveOptLog(optLog);

        return new ResponseInfo<Object>();
    }
    
    @ApiOperation("移动分组")
    @PostMapping("/move-group")
    public ResponseInfo<Object> moveGroup(@ApiParam(value = "收藏参数") @Valid @RequestBody CollectInfoReqVO collectVO){
        CollectInfo collectInfo = new CollectInfo();
        BeanUtils.copyProperties(collectVO,collectInfo);
        collectInfoService.modifyById(collectInfo);
        return new ResponseInfo<Object>();
    }
    
    @ApiOperation("分组列表-暂")
    @PostMapping("/list-collect_group")
    public ResponseInfo<List<CollectGroupVO>> listCollectGroup(){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        List<CollectGroupVO> collectGroupVOS = collectService.countGroup(loginUser.getId());
        return new ResponseInfo<List<CollectGroupVO>>(collectGroupVOS);
    }
    
    @ApiOperation("添加分组-暂")
    @PostMapping("/add-group")
    public ResponseInfo<Object> addGroup(@ApiParam(value = "分组参数") @Valid @RequestBody ViewGroupVO group){
        CollectGroup cg = new CollectGroup();
        cg.setName(group.getName());
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        cg.setCreateUserId(loginUser.getId());
        ResponseInfo<Object> responseInfo = new ResponseInfo<>();
        OptLogReqVO vo1 = new OptLogReqVO();
        vo1.setOptContent("添加"+group.getName()+"分组");
        vo1.setOptType(6);
        vo1.setOptModule(11);
        if(collectGroupService.queryList(cg).isEmpty()) {
            Long id = myIdGenerator.nextId();
            CollectGroup collectGroup = new CollectGroup();
            BeanUtils.copyProperties(group, collectGroup);
            collectGroup.setId(id);
            collectGroup.setCreateTime(new Date());
            collectGroup.setCreateUserId(loginUser.getId());
            collectGroupService.add(collectGroup);
            ViewGroupVO vo = new ViewGroupVO();
            vo.setId(id);
            vo.setName(group.getName());
            responseInfo.setData(vo);
            vo1.setStatus(1);
        }else {
            responseInfo.setMessage("分组名已存在");
            responseInfo.setCode("1");
            vo1.setStatus(2);
        }
        logService.saveOptLog(vo1);
        return responseInfo;
    }
    
    @ApiOperation("删除分组-暂")
    @PostMapping("/delete-group")
    public ResponseInfo<Object> deleteGroup(@ApiParam(value = "分组参数") @Valid @RequestBody DeleteGroupVO group){
        collectService.deleteByGroupId(group.getId());
        collectGroupService.deleteById(group.getId());
        return new ResponseInfo<>();
    }
    
    @ApiOperation("编辑分组-暂")
    @PostMapping("/update-group")
    public ResponseInfo<Object> updateGroup(@ApiParam(value = "分组参数") @Valid @RequestBody ViewGroupVO group){
        CollectGroup collectGroup = new CollectGroup();
        BeanUtils.copyProperties(group,collectGroup);
        collectGroupService.modifyById(collectGroup);
        return new ResponseInfo<>();
    }
    
}
